﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Printing;
using MySql.Data.MySqlClient;


namespace Warehouse
{
    public partial class Form2 : Form
    {
        string _connectionString;
        DBConnect _db;
        String _warehouse = "";
        String _ip = "";

        public Form2(String _toPrint, String _server, String _cabang)
        {
            _ip = _server;
            _warehouse = _cabang;
            InitializeComponent();
            printSPKB(_toPrint);
        }

        /**
         * Transfer hasil mysql query ke dalam DataTable spy bisa di show ke user
         * @param _cmd (bwt open connection ke database)
         * @param _txt (string query perintah bwt jalanin query mysql)
         */
        private DataTable SQLCommand(MySqlCommand _cmd, string _txt)
        {
            _cmd.CommandText = _txt;
            DataSet _ds = new DataSet();
            MySqlDataAdapter _adap = new MySqlDataAdapter(_cmd);
            _adap.Fill(_ds);
            DataTable myDataTable = _ds.Tables[0];
            return myDataTable;
        }

        /**
         * Show data2 SPKB bwt di print
         * @param _toPrint (nomor surat yg akan di print)
         */
        private void printSPKB(String _toPrint)
        {
            _db = new DBConnect(_ip, "warehouse_" + _warehouse);
            _connectionString = _db.getDBString();
            DataTable _dt = new DataTable();
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            _dt = SQLCommand(_command, "SELECT Barcode, Nama, daftar_warna.Warna, Jumlah, Dari FROM surat_jalan_detail INNER JOIN barcode_barang ON surat_jalan_detail.Barang = barcode_barang.Barcode INNER JOIN list_barang ON barcode_barang.Barang = list_barang.Kode INNER JOIN daftar_warna ON barcode_barang.Warna = daftar_warna.Kode WHERE Nomor = '" + _toPrint + "'");
            dataGridView1.DataSource = _dt;
            _dt = SQLCommand(_command, "SELECT surat_jalan.Nomor, Ke FROM surat_jalan INNER JOIN surat_jalan_detail ON surat_jalan.Nomor = surat_jalan_detail.Nomor WHERE surat_jalan.Nomor = '" + _toPrint + "'");
            DataRow _tempRow = null;
            int _height = dataGridView1.Rows[0].Height;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                textBox1.Text = _tempRow["Nomor"].ToString();
                textBox2.Text = _tempRow["Ke"].ToString();
                dataGridView1.Height += _height;
                this.Height += _height;
            }
        }

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        private static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);

        private Bitmap memoryImage;
        private void CaptureScreen()
        {
            Graphics mygraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            PrintDocument myDoc = new PrintDocument();
            PrintPreviewDialog print_dlg = new PrintPreviewDialog();
            print_dlg.Document = printDocument1;
            print_dlg.ShowDialog();
            this.Close();
        }
    }
}
